package router

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func Cors(c *gin.Context) {
	method := c.Request.Method
	origin := c.Request.Header.Get("Origin")
	c.Header("Access-Control-Allow-Origin", origin)
	c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token,X-Token,X-User-Id")
	c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS,DELETE,PUT")
	c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type, New-Token, New-Expires-At")
	c.Header("Access-Control-Allow-Credentials", "true")
	// 放行所有OPTIONS方法
	if method == "OPTIONS" {
		c.AbortWithStatus(http.StatusNoContent)
	}
	// 处理请求
	c.Next()
}

func Routers() *gin.Engine {

	r := gin.Default()
	//允许跨域请求配置
	r.Use(Cors)
	//路由的初始化
	//user crud
	UserGroup := RouterGroupApp.UserRouterGroup
	UserGroup.InitUserRouter(r)
	//ns  crud
	k8sNameSpaceGroup := RouterGroupApp.K8SNameSpaceRouterGroup
	k8sNameSpaceGroup.InitK8SNameSpaceRouter(r)
	//pods  crud
	k8sPodGroup := RouterGroupApp.K8SPodRouterGroup
	k8sPodGroup.InitK8SPodRouter(r)
	//
	k8sClusterGroup := RouterGroupApp.K8SClusterRouterGroup
	k8sClusterGroup.InitK8SClusterRouter(r)
	K8SCronJobRouterGroup := RouterGroupApp.K8SCronJobRouterGroup
	K8SCronJobRouterGroup.InitK8SCronJobRouter(r)

	k8sDeploymentGroup := RouterGroupApp.K8SDeploymentRouterGroup
	k8sDeploymentGroup.InitK8SDeploymentRouter(r)

	k8sDeploymentsGroup := RouterGroupApp.K8SDeploymentsRouterGroup
	k8sDeploymentsGroup.InitK8SDeploymentsRouter(r)

	k8sDaemonSetGroup := RouterGroupApp.K8SDaemonSetRouterGroup
	k8sDaemonSetGroup.InitK8SDaemonSetRouter(r)

	k8sServiceGroup := RouterGroupApp.K8sServiceRouterGroup
	k8sServiceGroup.InitServicesRouter(r)

	k8sJobGroup := RouterGroupApp.K8SJobRouterGroup
	k8sJobGroup.InitK8SJobRouter(r)
	return r
}
